package com.isoftstone.merchant.service.feeUser.impl;

import com.isoftstone.common.Page;
import com.isoftstone.merchant.dao.feeUser.MFeeUserMapper;
import com.isoftstone.merchant.dao.mdept.MDeptMapper;
import com.isoftstone.merchant.entity.feeUser.MFeeUser;
import com.isoftstone.merchant.entity.mdept.MDept;
import com.isoftstone.merchant.service.feeUser.FeeUserService;
import com.isoftstone.util.PageData;
import com.isoftstone.util.UuidUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by as on 2017/12/4.
 */
@Service
public class FeeUserServiceImpl implements FeeUserService{
    @Autowired
    MFeeUserMapper mFeeUserMapper;
    @Autowired
    MDeptMapper mDeptMapper;

    private List<MDept>list=new ArrayList<MDept>();

    public List<MFeeUser> getFeeUserlistPage(Page page) throws Exception {
        return mFeeUserMapper.selectFeeUserlistPage(page);
    }

    @Override
    public Integer getFeeUserCountFromPayment(String feeUserId,String feeStatus,String status) throws Exception {
        return mFeeUserMapper.selectFeeUserCountFromPayment(feeUserId,feeStatus,status);
    }

    @Override
    @Transactional
    public Integer deleteFeeUserCountFromPayment(String feeUserId) throws Exception {
        return mFeeUserMapper.deleteFeeUserCountFromPayment(feeUserId);
    }

    @Override
    @Transactional
    public void deleteFeeUserById(String feeUserId) throws Exception {
        mFeeUserMapper.deleteFeeUser(feeUserId);
    }

    @Override
    public MFeeUser getFeeUserById(String feeUserId) throws Exception {
        return mFeeUserMapper.selectByPrimaryKey(feeUserId);
    }

    @Override
    public List<MFeeUser> getExcelAllFeeUserList( Map<String,Object> map) throws Exception {
        return mFeeUserMapper.selectExcelAllFeeUserList(map);
    }

    @Override
    @Transactional
    public Integer updateFeeUserStatus(PageData pd) throws Exception {
        return mFeeUserMapper.updateFeeUserStatus(pd);
    }

    @Transactional
    public void saveFeeUserByExcel(MFeeUser mus) throws Exception {
        mFeeUserMapper.insertSelective(mus);
    }

    @Override
    public String findFeeUserIdByFeeId(MFeeUser mFeeUser){
        return mFeeUserMapper.selectFeeUserIdByFeeId(mFeeUser);
    }

    @Override
    public List<MFeeUser> getHandFeeUserlistPage(Page page) throws Exception {
        return mFeeUserMapper.getHandFeeUserlistPage(page);
    }

    @Override
    public List<MFeeUser> getFeeUserlistPageDept(List<MFeeUser> feeUsertList0) throws Exception {
        try {
            for(MFeeUser mFeeUser:feeUsertList0){
                String deptId=mFeeUser.getDeptId();
                getdeptStr(deptId);
                if(list!=null){///////////////
                   for(MDept mDept:list){
                       String str=String.valueOf(mDept.getLevel());
                       if("1".equals(str)){
                           mFeeUser.setOneDept(mDept.getDeptName());
                       }else if("2".equals(str)){
                           mFeeUser.setTwoDept(mDept.getDeptName());
                       }else if("3".equals(str)){
                           mFeeUser.setThreeDept(mDept.getDeptName());
                       }else if("4".equals(str)){
                           mFeeUser.setFourDept(mDept.getDeptName());
                       }else if("5".equals(str)){
                           mFeeUser.setFiveDept(mDept.getDeptName());
                       }else if("6".equals(str)){
                           mFeeUser.setSixDept(mDept.getDeptName());
                       }else if("7".equals(str)){
                           mFeeUser.setSevenDept(mDept.getDeptName());
                       }else if("8".equals(str)){
                           mFeeUser.setEightDept(mDept.getDeptName());
                       }else if("9".equals(str)){
                           mFeeUser.setNineDept(mDept.getDeptName());
                       }else if("10".equals(str)){
                           mFeeUser.setTenDept(mDept.getDeptName());
                       }
                   }
                  list.clear();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return feeUsertList0;
    }

    @Override
    public MFeeUser getCountFeeUser(Page page) throws Exception {
        return mFeeUserMapper.getCountFeeUser(page);
    }

    @Override
    public Integer getAllUserNumber(Page page) throws Exception {
        return mFeeUserMapper.getAllUserNumber(page);
    }

    @Override
    public Integer getTodayUserNumber(Page page) throws Exception {
        return mFeeUserMapper.getTodayUserNumber(page);
    }

    @Override
    public List<MFeeUser> getFeeUserByDeptId(String deptId) throws Exception {
        return mFeeUserMapper.getFeeUserByDeptId(deptId);
    }

    @Override
    public List<MDept> getalldeptByUserId( String userId) throws Exception {
        return mDeptMapper.getalldeptByUserId(userId);
    }

    //递归查询所在所有部门
    private String  getdeptStr(String deptId){
        try {
            MDept dept=mDeptMapper.getdeptById(deptId);
            if(dept!=null) {
                if ("0".equals(dept.getSuperDeptId())) {
                    list.add(dept);
                    return null;
                } else {
                    list.add(dept);
                    return getdeptStr(dept.getSuperDeptId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<MDept> getalldept(String deptId){
        getdeptStr(deptId);
        return list;
    }
    @Override
    public Integer getFeeUserId2(String feeId, String feeUserId) throws Exception {
        return mFeeUserMapper.selectFeeId2(feeId,feeUserId);
    }

    @Override
    public Integer getFeeUserId(String feeId,String cropId) throws Exception {
        return mFeeUserMapper.selectFeeId(feeId,cropId);
    }

    @Override
    public Integer getFeeUserId1(Map<String, Object> feeId) throws Exception {
        return mFeeUserMapper.selectFeeId1(feeId);
    }

    @Override
    @Transactional
    public void updateFeeUser(MFeeUser mFuser) throws Exception {
        mFeeUserMapper.updateByPrimaryKeySelective(mFuser);
    }

    @Override
    public List<MFeeUser> queryFeeIdByCropId(String cropId, String openAccount) throws Exception {
        return mFeeUserMapper.queryFeeIdByCropId(cropId,openAccount);
    }

    @Override
    public MFeeUser queryFeeUserByFeeId(@Param("feeId") String feeId, @Param("corpId") String corpId) throws Exception {
        return mFeeUserMapper.queryFeeUserByFeeId(feeId,corpId);
    }


    @Override
    public MFeeUser queryFeeUserByCorpIdAndFeeId(String feeId, String cropId) throws Exception {
        return mFeeUserMapper.queryFeeUserByCorpIdAndFeeId(feeId,cropId);
    }

    @Override
    public List<MFeeUser> getFeeIdByCropId(String cropId) throws Exception {
        return mFeeUserMapper.getFeeIdByCropId(cropId);
    }

    @Override
    public MFeeUser getFeeUserByCorpIdAndFeeId(@Param("feeId") String feeId, @Param("cropId") String cropId) throws Exception {
        return mFeeUserMapper.getFeeUserByCorpIdAndFeeId(feeId,cropId);
    }

    @Override
    public void cancelContract(@Param("feeId") String feeId, @Param("cropId") String cropId, @Param("status") String status) throws Exception {
        mFeeUserMapper.cancelContract(feeId,cropId,status);
    }

    @Override
    public void addFeeUser(MFeeUser feeUser) throws Exception {
        //待审核状态
        feeUser.setFeeUserId(UuidUtil.getNumFormatUUID());
        feeUser.setStatus("2");
        feeUser.setCreateDate(new Date());
        //删除标记：0：正常  1：无效
        feeUser.setIsDel("0");
        mFeeUserMapper.addFeeUser(feeUser);
    }
    @Override
    public void extentContract(String feeId,  String corpId, String status) throws Exception {
        mFeeUserMapper.extentContract(feeId,corpId,status);
    }

    @Override
    public MFeeUser getfeeUserByFeeId(String feeId,String corpId) throws Exception {
        return mFeeUserMapper.getfeeUserByFeeId(feeId,corpId);
    }

    @Override
    public Integer getAllUserNumbers(PageData allUserPageDate) throws Exception {
        return mFeeUserMapper.getAllUserNumbers(allUserPageDate);
    }

    @Override
    public Integer getTodayUserNumbers(PageData todayUserPageDate) throws Exception {
        return mFeeUserMapper.getTodayUserNumbers(todayUserPageDate);
    }
}
